/**
 * 运行npm run getRouter
 * 读取page文件目录自动生成路由列表
 */
const fs = require('fs')
const path = require('path')

function getRouter(pagePath) {
  fs.readdir(pagePath, (e, file) => {
    let str = `interface routerItem {\n  path: string;\n  name: string;\n  component: () => Promise<typeof import("*.vue")>;\n}\nexport default [\n`
    let data = ''
    file.forEach((item, index) => {
      let itemStr = `  {path: '/${item}',name: '${item}',component: () => import('@src/page/${item}/index.vue')},\n`
      if (file.length - 1 === index) itemStr = itemStr + ']'
      data += itemStr
    })
    fs.writeFileSync(path.resolve(__dirname, '../src/router/routerList.ts'), str + data + ` as routerItem[]`)
  })
}

getRouter(path.resolve(__dirname, '../src/page'))